<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css">

    <style>
        .layui-table-cell {
            /* height: 60px;
            line-height: 60px; */
            height: auto !important;
            white-space: normal;
            padding: 0px;
        }

        th .layui-table-cell {
            height: 30px;
            line-height: 30px;
        }

        th .laytable-cell-1-0-7 {
            height: 30px;
            line-height: 30px;
        }

        .layui-table img {
            width: 100px;
            height: 60px;
        }
    </style>

</head>
<body>

<fieldset class="table-search-fieldset">
    <legend>搜索信息</legend>
    <div style="margin: 10px 10px 10px 10px">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">酒水ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="did" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">酒水类型</label>
                    <div class="layui-input-inline">
                        <input type="text" name="dtype" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">酒水价格</label>
                    <div class="layui-input-inline">
                        <input type="text" name="dprice" autocomplete="off" class="layui-input">
                    </div>
                </div>
    
                <div class="layui-inline">
                    <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="data-search-btn"><i
                            class="layui-icon"></i> 搜 索
                    </button>
                </div>
            </div>
        </form>
    </div>
</fieldset>


<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script type="text/html" id="dphoto">
    <!-- 这里的 checked 的状态只是演示 -->
    <img src="{{d.dphoto}}" name="dphoto" alt="">
</script>


<script src="/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use(['table', "form"] , function () {
        var table = layui.table, $ = layui.jquery, form=layui.form;

        table.render({
            elem: '#test'
            , url: 'http://localhost:8080/drinks/pageData'/*tpa=https://www.layuiweb.com/test/table/demo1.json*/
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox'}
                , {field: 'did', title: '酒水ID', width: 80, unresize: true, sort: true}
                , {field: 'dtype', title: '酒水类型', width: 120, edit: 'text'}
                , {field: 'dprice', title: '酒水价格', width: 100}
                , {field: 'dquantity', title: '酒水数量', width: 120}
                , {field: 'dphoto', title: '酒水实图', width: 100, templet: '#dphoto'}
                , {field: 'dintroduce', title: '酒水简介'}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('test', {
                page: {
                    curr: 1
                }
                , where: {
                    //searchParams: result
                    "did": data.field.did,
                    "dtype": data.field.dtype,
                    "dprice": data.field.dprice,
                }
            }, 'data');

            return false;
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                case 'add':
                    layer.open({
                        title: "添加酒水",
                        shade: 0.4,
                        type: 2,
                        shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                        area: ['50%', '50%'],
                        fixed: true, //不固定
                        maxmin: true,
                        content: './drinksadd.html',
                        end: function (index) {
                            table.reload('test');
                        }
                    });
                    break;

                case 'delete':
                    layer.msg('delete');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('酒虽好，但切勿贪杯哦~~~');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            "url": 'http://localhost:8080/drinks/delete',
                            "data": {
                                "did": data.did
                            },
                            "success": function (result) {
                                if (result.code == 200) {
                                    layer.msg("删除成功");
                                    obj.del();
                                } else {
                                    layer.msg("删除失败");
                                }
                            }
                        });

                        layer.close(index);

                    });
            } else if (obj.event === 'edit') {
                // layer.prompt({
                //     formType: 2
                //     ,value: data.email
                // }, function(value, index){
                //     obj.update({
                //         email: value
                //     });
                //     layer.close(index);
                // });
                // 把数据存到 localStorage
                localStorage.setItem("drinks", JSON.stringify(data));
                layer.open({
                    title: "编辑酒水信息",
                    shade: 0.3,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['50%', '50%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: './drinksredit.html',
                    end: function (index) {
                        table.reload('test');
                    }
                });
            }
        });
    });
</script>

</body>
</html>